import { createWebHistory, createRouter } from 'vue-router'

const routes = [
    {
        path: '/',
        redirect:'/login',
    },
    {
        name: 'login',
        path: '/login',
        component: () => import('@/views/login/Login'),
    },
    {
        name: 'study',
        path: '/study',
        component: () => import('@/components/Menu'),
        redirect: '/home',
        children:[
            {
                name: 'home',
                path: '/home',
                component: () => import('@/views/home/HomeContent'),
            },
            {
                name: 'test1',
                path: '/test1',
                component: () => import('@/views/nav/NavTest1'),
            },
            {
                name: 'test2',
                path: '/test2',
                component: () => import('@/views/nav/NavTest2'),
            },
        ],
    },
]

const router = createRouter({
    // 路由的模式
    history: createWebHistory(),
    // 路由规则
    routes
})


//全局路由守卫
router.beforeEach((to, from, next) => {

    if(to.path==='/login'){
        next();
    }else{
        if (localStorage.getItem('loginData')) {
            next()
        } else {
            router.push({name:'login'});
        }
    }
})

export default router
